package com.engine.aecc.comm.web;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-04-24
 * @Description: 统一自定义获取公共信息接口
 * @ModifyLog:
 **/

public class WeaCusInfoWeb {
    /**
     * @Description: 获取参数用户信息
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getUserInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getUserInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            String userid = Util.null2String(request.getParameter("userid"));
            Map<String, String> map = new HashMap<>();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            map.put("workcode", resourceComInfo.getWorkcode(userid));
            map.put("lastname", resourceComInfo.getLastname(userid));
            map.put("loginid", resourceComInfo.getLoginID(userid));
            map.put("managerid", resourceComInfo.getManagerID(userid));
            map.put("subcompanyid", resourceComInfo.getSubCompanyID(userid));
            map.put("status", resourceComInfo.getStatus(userid));
            String departmentid = resourceComInfo.getDepartmentID(userid);
            map.put("departmentid", departmentid);
            map.put("departmentname", departmentComInfo.getDepartmentname(departmentid));
            map.put("departmentcode", departmentComInfo.getDepartmentCode(departmentid));
            apidatas.put("data", map);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("error_message", "请求接口异常");
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * @Description: 获取流程信息参数
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2024/04/24
     */
    @GET
    @Path("/getWorkflowInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getWorkflowInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        try {
            String tablename = Util.null2String(request.getParameter("tablename"));
            Map<String, String> map = new HashMap<>();
            rs.executeQuery("select distinct t1.workflowname,t1.formid,ifnull(t1.activeversionid,t1.id) workflowid" +
                    "from workflow_base t1 join workflow_bill t2 on t1.formid = t2.id  where t2.tablename=?",tablename);
            if (rs.next()) {
                map.put("workflowname", Util.null2String(rs.getString("workflowname")));
                map.put("formid", Util.null2String(rs.getString("formid")));
                map.put("workflowid", Util.null2String(rs.getString("workflowid")));
            }
            apidatas.put("data", map);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("error_message", "请求接口异常");
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * @Description: 获取建模信息参数
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2024/04/24
     */
    @GET
    @Path("/getModeInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getModeInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        RecordSet rs = new RecordSet();
        try {
            List<Map<String, String>> mapList = new ArrayList<>();
            String tablename = Util.null2String(request.getParameter("tablename"));
            rs.executeQuery("select t1.id customid,t1.modeid, t1.customname,t1.customdesc, t1.formid from " +
                    "mode_customsearch t1 join workflow_bill t2 on t1.formid=t2.id  where t2.tablename =?", tablename);
            while (rs.next()) {
                Map<String, String> map = new HashMap<>();
                map.put("workflowname", Util.null2String(rs.getString("workflowname")));
                map.put("formid", Util.null2String(rs.getString("formid")));
                map.put("workflowid", Util.null2String(rs.getString("workflowid")));
                mapList.add(map);
            }
            apidatas.put("data", mapList);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("error_message", "请求接口异常");
        }
        return JSONObject.toJSONString(apidatas);
    }
}

